<?php
namespace ZN\Services\Crontab;

trait IntervalTrait
{
	//----------------------------------------------------------------------------------------------------
	//
	// Yazar      : Ozan UYKUN <ozanbote@windowslive.com> | <ozanbote@gmail.com>
	// Site       : www.zntr.net
	// Lisans     : The MIT License
	// Telif Hakkı: Copyright (c) 2012-2016, zntr.net
	//
	//----------------------------------------------------------------------------------------------------
	
	//----------------------------------------------------------------------------------------------------
	// Interval
	//----------------------------------------------------------------------------------------------------
	// 
	// @var string
	//
	//----------------------------------------------------------------------------------------------------
	protected $interval = '* * * * *';
	
	//----------------------------------------------------------------------------------------------------
	// Minute
	//----------------------------------------------------------------------------------------------------
	// 
	// @var string
	//
	//----------------------------------------------------------------------------------------------------
	protected $minute = '*';
	
	//----------------------------------------------------------------------------------------------------
	// Hour
	//----------------------------------------------------------------------------------------------------
	// 
	// @var string
	//
	//----------------------------------------------------------------------------------------------------
	protected $hour = '*';
	
	//----------------------------------------------------------------------------------------------------
	// Day Number
	//----------------------------------------------------------------------------------------------------
	// 
	// @var string
	//
	//----------------------------------------------------------------------------------------------------
	protected $dayNumber = '*';
	
	//----------------------------------------------------------------------------------------------------
	// Month
	//----------------------------------------------------------------------------------------------------
	// 
	// @var string
	//
	//----------------------------------------------------------------------------------------------------
	protected $month = '*';
	
	//----------------------------------------------------------------------------------------------------
	// Day
	//----------------------------------------------------------------------------------------------------
	// 
	// @var string
	//
	//----------------------------------------------------------------------------------------------------
	protected $day = '*';
	
	//----------------------------------------------------------------------------------------------------
	// Month Format
	//----------------------------------------------------------------------------------------------------
	// 
	// @var array
	//
	//----------------------------------------------------------------------------------------------------
	protected $monthFormat = array
	(
		'janury'    => 1,
		'february'  => 2,
		'march'		=> 3,
		'april'		=> 4,
		'may'		=> 5,
		'june'		=> 6,
		'july'		=> 7,
		'august'	=> 8,
		'september' => 9,
		'october'	=> 10,
		'november'	=> 11,
		'december'	=> 12
	);
	
	//----------------------------------------------------------------------------------------------------
	// Day Format
	//----------------------------------------------------------------------------------------------------
	// 
	// @var array
	//
	//----------------------------------------------------------------------------------------------------
	protected $dayFormat = array
	(
		'sunday'    => 0,
		'monday'  	=> 1,
		'tuesday'	=> 2,
		'wednesday'	=> 3,
		'thursday'	=> 4,
		'friday'	=> 5,
		'saturday'	=> 6
	);
	
	//----------------------------------------------------------------------------------------------------
	// Protected Slashes
	//----------------------------------------------------------------------------------------------------
	// 
	// @param  string $data
	// @return string
	//
	//----------------------------------------------------------------------------------------------------
	protected function _slashes($data)
	{
		if( $data[0] === '/' )
		{
			return prefix($data, '*');	
		}
		
		return $data;
	}
	
	//----------------------------------------------------------------------------------------------------
	// Hourly
	//----------------------------------------------------------------------------------------------------
	// 
	// @param  string void
	// @return object
	//
	//----------------------------------------------------------------------------------------------------
	public function hourly()
	{
		$this->interval = '0 * * * *';
		
		return $this;
	}
	
	//----------------------------------------------------------------------------------------------------
	// Daily
	//----------------------------------------------------------------------------------------------------
	// 
	// @param  string void
	// @return object
	//
	//----------------------------------------------------------------------------------------------------
	public function daily()
	{
		$this->interval = '0 0 * * *';
		
		return $this;
	}
	
	//----------------------------------------------------------------------------------------------------
	// Midnight
	//----------------------------------------------------------------------------------------------------
	// 
	// @param  string void
	// @return object
	//
	//----------------------------------------------------------------------------------------------------
	public function midnight()
	{
		$this->daily();
		
		return $this;
	}
	
	//----------------------------------------------------------------------------------------------------
	// Monthly
	//----------------------------------------------------------------------------------------------------
	// 
	// @param  string void
	// @return object
	//
	//----------------------------------------------------------------------------------------------------
	public function monthly()
	{
		$this->interval = '0 0 1 * *';
		
		return $this;
	}
	
	//----------------------------------------------------------------------------------------------------
	// Weekly
	//----------------------------------------------------------------------------------------------------
	// 
	// @param  string void
	// @return object
	//
	//----------------------------------------------------------------------------------------------------
	public function weekly()
	{
		$this->interval = '0 0 * * 0';
		
		return $this;
	}
	
	//----------------------------------------------------------------------------------------------------
	// Yearly
	//----------------------------------------------------------------------------------------------------
	// 
	// @param  string void
	// @return object
	//
	//----------------------------------------------------------------------------------------------------
	public function yearly()
	{
		$this->interval = '0 0 1 1 *';
		
		return $this;
	}
	
	//----------------------------------------------------------------------------------------------------
	// Annualy
	//----------------------------------------------------------------------------------------------------
	// 
	// @param  string void
	// @return object
	//
	//----------------------------------------------------------------------------------------------------
	public function annualy()
	{
		$this->yearly();
		
		return $this;
	}
	
	//----------------------------------------------------------------------------------------------------
	// Clock
	//----------------------------------------------------------------------------------------------------
	// 
	// @param  string $clock: 24:59
	// @return object
	//
	//----------------------------------------------------------------------------------------------------
	public function clock($clock = '23:59')
	{
		$match = '[0-9]{1,2}';
		
		if( ! preg_match('/^'.$match.':'.$match.'$/', $clock) )
		{
			\Errors::set('Services', 'crontabTimeFormatError');
		}
		else
		{
			$clockEx  = explode(':', $clock);
			
			$this->minute($clockEx[1]);
			$this->hour($clockEx[0]);
		}
		
		return $this;
	}
	
	//----------------------------------------------------------------------------------------------------
	// Minute
	//----------------------------------------------------------------------------------------------------
	// 
	// @param  string $minute: *
	// @return object
	//
	//----------------------------------------------------------------------------------------------------
	public function minute($minute = '*')
	{
		$this->minute = $this->_slashes($minute);
		return $this;
	}
	
	//----------------------------------------------------------------------------------------------------
	// Hour
	//----------------------------------------------------------------------------------------------------
	// 
	// @param  string $hour: *
	// @return object
	//
	//----------------------------------------------------------------------------------------------------
	public function hour($hour = '*')
	{
		$this->hour = $this->_slashes($hour);
		return $this;
	}
	
	//----------------------------------------------------------------------------------------------------
	// Day Number
	//----------------------------------------------------------------------------------------------------
	// 
	// @param  string $dayNumber: *
	// @return object
	//
	//----------------------------------------------------------------------------------------------------
	public function dayNumber($dayNumber = '*')
	{
		$this->dayNumber = $this->_slashes($dayNumber);
		return $this;
	}
	
	//----------------------------------------------------------------------------------------------------
	// Protected Format
	//----------------------------------------------------------------------------------------------------
	// 
	// @param  string $varname: empty
	// @param  string $objectname: empty
	// @param  string $data: empty
	// @return void
	//
	//----------------------------------------------------------------------------------------------------
	protected function _format($varname, $objectname, $data)
	{
		$format      = $this->$varname;
		$replaceData = str_ireplace(array_keys($format), array_values($format), $data);
		
		$this->$objectname = $this->_slashes($replaceData);
	}
	
	//----------------------------------------------------------------------------------------------------
	// Month Number
	//----------------------------------------------------------------------------------------------------
	// 
	// @param  string $monthNumber: *
	// @return object
	//
	//----------------------------------------------------------------------------------------------------
	public function month($monthNumber = '*')
	{
		$this->_format('monthFormat', __FUNCTION__, $monthNumber );
		
		return $this;
	}
	
	//----------------------------------------------------------------------------------------------------
	// Day
	//----------------------------------------------------------------------------------------------------
	// 
	// @param  string $day: *
	// @return object
	//
	//----------------------------------------------------------------------------------------------------
	public function day($day = '*')
	{
		$this->_format('dayFormat', __FUNCTION__, $day);
		
		return $this;
	}
	
	//----------------------------------------------------------------------------------------------------
	// Interval
	//----------------------------------------------------------------------------------------------------
	// 
	// @param  string $interval: * * * * *
	// @return object
	//
	//----------------------------------------------------------------------------------------------------
	public function interval($interval = '* * * * *')
	{
		$this->interval = $interval;	
		return $this;
	}
}